package com.yilin.tms.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.util.DateTimeUtil;
import com.yilin.commons.util.MathUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.user.account.AdvanceMoney;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.remote.IVehicleRemote;
import com.yilin.tms.user.mapper.AdvanceMoneyMapper;
import com.yilin.tms.user.service.IAdvanceMoneyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
public class AdvanceMoneyService extends ServiceImpl<AdvanceMoneyMapper, AdvanceMoney> implements IAdvanceMoneyService {
    @Resource
    private AdvanceMoneyMapper advanceMoneyMapper;

    @Resource
    private IVehicleRemote vehicleRemote;
    @Resource
    private AccountService accountService;
   //添加或者修改支出记录
    @Override
    public void  saveOrUpdateAdvanceMoneySubtract(Account account, AdvanceMoney advanceMoney, TruckVehicle truckVehicle){
        advanceMoney.setAccountId(account.getId());
        advanceMoney.setAccountName(account.getName());
        advanceMoney.setOrganizeId(account.getOrganizeId());
        advanceMoney.setAdvanceState(AdvanceMoney.AdvanceState.audting);
        advanceMoney.setAdvanceType(AdvanceMoney.AdvanceType.expenditure);
        advanceMoney.setOrganizeName(account.getOrganizeName());
        advanceMoney.setSuperiorPlatId(account.getSuperiorPlatId());
        advanceMoney.setSuperiorPlatName(account.getSuperiorPlatName());
        if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.oill||advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.vehicle||advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.integral){
            advanceMoney.setTruckNo(truckVehicle.getTrackNo());
            advanceMoney.setTruckId(truckVehicle.getId());
            if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.vehicle){
                truckVehicle.setAdvanceMoney(MathUtil.subtract(truckVehicle.getAdvanceMoney(),advanceMoney.getMonery()));
            }else if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.oill){
                truckVehicle.setOillMoney(MathUtil.subtract(truckVehicle.getOillMoney(),advanceMoney.getMonery()));
            }else if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.integral){
                truckVehicle.setOillIntegral(MathUtil.subtract(truckVehicle.getOillIntegral(),advanceMoney.getMonery()));
            }
            ParamUtil.isSuccess(vehicleRemote.updateVehicle(truckVehicle));
        }else{
          account.setMonery(MathUtil.subtract(account.getMonery(),advanceMoney.getMonery()));
          accountService.updateById(account);
        }
        advanceMoney.setAdvanceNo(DateTimeUtil.dateNo("ZC"));
        saveOrUpdate(advanceMoney);
    }
    //添加或者收入
    @Override
    public void  saveOrUpdateAdvanceMoneyAdd(Account loginAccount, String accountId, AdvanceMoney advanceMoney, TruckVehicle truckVehicle){
        advanceMoney.setAdvanceState(AdvanceMoney.AdvanceState.success);
        advanceMoney.setAdvanceType(AdvanceMoney.AdvanceType.income);
        advanceMoney.setAuditingId(loginAccount.getId());
        advanceMoney.setAuditingName(loginAccount.getId());
        advanceMoney.setAuditingDate(new Date());
        if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.oill||advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.vehicle||advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.integral){
            advanceMoney.setAccountId(truckVehicle.getManageUserId());
            advanceMoney.setAccountName(truckVehicle.getManageUserName());
            advanceMoney.setTruckId(truckVehicle.getId());
            advanceMoney.setTruckNo(truckVehicle.getTrackNo());
            advanceMoney.setOrganizeId(truckVehicle.getOrganizeId());
            advanceMoney.setOrganizeName(truckVehicle.getOrganizeName());
            advanceMoney.setSuperiorPlatId(truckVehicle.getSuperiorPlatId());
            advanceMoney.setSuperiorPlatName(truckVehicle.getSuperiorPlatName());
            if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.vehicle){
                truckVehicle.setAdvanceMoney(MathUtil.add(truckVehicle.getAdvanceMoney(),advanceMoney.getMonery()));
            }else if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.oill){
                truckVehicle.setOillMoney(MathUtil.add(truckVehicle.getOillMoney(),advanceMoney.getMonery()));
            }else if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.integral){
                truckVehicle.setOillIntegral(MathUtil.add(truckVehicle.getOillIntegral(),advanceMoney.getMonery()));
            }
            ParamUtil.isSuccess(vehicleRemote.updateVehicle(truckVehicle));
        }else{
            ParamUtil.isNull(accountId,"用户获取失败");
            Account account=accountService.getAccountById(accountId);
            advanceMoney.setAccountId(account.getId());
            advanceMoney.setAccountName(account.getName());
            advanceMoney.setOrganizeId(account.getOrganizeId());
            advanceMoney.setOrganizeName(account.getOrganizeName());
            advanceMoney.setSuperiorPlatId(account.getSuperiorPlatId());
            advanceMoney.setSuperiorPlatName(account.getSuperiorPlatName());
            ParamUtil.isNull(account,"获取用户失败");
            account.setMonery(MathUtil.add(account.getMonery(),advanceMoney.getMonery()));
            accountService.updateById(account);
        }
        advanceMoney.setAdvanceNo(DateTimeUtil.dateNo("SR"));
        saveOrUpdate(advanceMoney);
}
    //审核
    @Override
    public void  auditingAdvanceMoney(Account account,AdvanceMoney advanceMoney){
        advanceMoney.setAuditingId(account.getId());
        advanceMoney.setAuditingName(account.getName());
        advanceMoney.setAuditingDate(new Date());
        saveOrUpdate(advanceMoney);
    }
    //根据ID查询
    @Override
    public AdvanceMoney  getAdvanceMoneyById(String id){
     return   advanceMoneyMapper.selectById(id);
    }
    /**
     * 获取分页
     */
    @Override
    public PageData<AdvanceMoney> getAdvanceMoneyPage(Account loginUser, AdvanceMoney query, Integer start, Integer limit) {
        QueryWrapper<AdvanceMoney> queryWrapper = new QueryWrapper<>();
        if (loginUser.getUserType() != UserType.platformUser && loginUser.getUserType() != UserType.adminUser) {
            queryWrapper.and(wrapper ->
                    wrapper.eq("organize_id", loginUser.getOrganizeId())
                            .or()
                            .eq("superior_plat_id", loginUser.getOrganizeId())
            );
        }
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return page(new PageData<>(start, limit), queryWrapper);
    }
}
